<!--
 * @Author: early-autumn
 * @Date: 2020-05-15 15:42:30
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-06-02 18:24:57
-->
<template>
  <div>
    <el-table v-loading="loading" border stripe :data="entries">
      <el-table-column
        prop="yiyaBillingMain.clinic"
        label="操作诊所"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="yiyaBillingMain.firstBilling"
        label="首次开单时间"
        align="center"
        min-width="120"
      ></el-table-column>
      <el-table-column
        prop="yiyaMainClosingEntries.createTime"
        label="开单时间"
        min-width="150"
        align="center"
      ></el-table-column>
      <el-table-column prop="billStatus" label="交易类型" align="center">
        <template slot-scope="scope">
          {{ billingTypeEnum[scope.row.yiyaMainClosingEntries.type] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="yiyaBillingMain.tc"
        label="TC"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="doctor"
        label="医生"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="yiyaMainClosingEntries.billingPerson"
        label="开单人"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="yiyaMainClosingEntries.thisPay"
        label="本次缴费"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="yiyaMainClosingEntries.billingRemarks"
        label="备注"
        align="center"
      ></el-table-column>
      <el-table-column prop="name" label="操作" align="center">
        <el-button slot-scope="scope" type="text" @click="handlePay(scope.row)">
          结账
        </el-button>
      </el-table-column>
      <empty slot="empty"></empty>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getEntriesPro"
    ></pagination>
    <payment-operation
      v-model="paymentOperation.visible"
      :data="paymentOperation.data"
      @ok="getEntriesPro"
    ></payment-operation>
    <repayment
      v-model="repayment.visible"
      :data="repayment.data"
      @ok="getEntriesPro"
    ></repayment>
    <refund-audit
      v-model="refundAudit.visible"
      :data="refundAudit.data"
      @ok="getEntriesPro"
    ></refund-audit>
  </div>
</template>

<script>
import { getEntriesPro } from '@/api/services/entries'
import { deepCopy } from '@/utils/utils'
import {
  billStatusEnum,
  billingStatusEnum,
  billingTypeEnum,
} from '@/utils/enums'
import { billDataTransform } from '../utils'

import Empty from '@/components/yiya/Empty'
import PaymentOperation from './PaymentOperation'
import Repayment from './Repayment'
import RefundAudit from './RefundAudit'

const QUERY_PARAMS = {
  pageNum: 1,
  pageSize: 10,
  billStatus: billingStatusEnum['已开单'],
}

export default {
  name: 'NonClosingRecordPane',
  components: {
    Empty,
    PaymentOperation,
    Repayment,
    RefundAudit,
  },
  props: {
    patient: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      loading: false,
      total: 0,
      queryParams: {},
      entries: [],
      totalMap: new Map(),
      paymentOperation: {
        visible: false,
        data: { yiyaBillingMain: {}, total: {} },
      },
      repayment: {
        visible: false,
        data: { yiyaBillingMain: {}, total: {} },
      },
      refundAudit: {
        visible: false,
        data: { yiyaBillingMain: {}, total: {} },
      },
    }
  },
  computed: {
    billStatusEnum() {
      return billStatusEnum
    },
    billingTypeEnum() {
      return billingTypeEnum
    },
  },
  mounted() {
    this.queryParams = {
      ...QUERY_PARAMS,
      clinicPersonId: this.patient.id,
    }
    this.getEntriesPro()
  },
  methods: {
    async getEntriesPro() {
      try {
        this.loading = true
        const { rows, total } = await getEntriesPro(this.queryParams)
        const { dataList, totalMap } = billDataTransform(rows)

        this.entries = dataList
        this.totalMap = totalMap
        this.total = total
      } finally {
        this.loading = false
      }
    },
    async handlePay(data) {
      if (data.yiyaMainClosingEntries.type === billingTypeEnum['项目开单']) {
        this.paymentOperation = {
          visible: true,
          data: deepCopy(data),
        }
      } else if (
        data.yiyaMainClosingEntries.type === billingTypeEnum['还欠款']
      ) {
        this.repayment = {
          visible: true,
          data: deepCopy(data),
        }
      } else if (data.yiyaMainClosingEntries.type === billingTypeEnum['退款']) {
        this.refundAudit = {
          visible: true,
          data: deepCopy(data),
        }
      }
    },
  },
}
</script>

<style></style>
